﻿namespace Borg
{
    public abstract class Behavior
    {
        public Slot BehaviorSlot { get; private set; }
        protected Runtime Runtime { get; private set; }

        internal void Bind(Slot slot)
        {
            BehaviorSlot = slot;
            Runtime = slot.Runtime;
            Bind();
        }

        internal void Bind(Composer composer)
        {
            Runtime = composer.Runtime;
            Bind();
        }

        internal void Unbind(Slot slot)
        {
            Unbind();
            Runtime = null;
            BehaviorSlot = null;
        }

        internal void Unbind(Composer composer)
        {
            Unbind();
            Runtime = null;
        }

        protected virtual void Bind()
        { }

        protected virtual void Unbind()
        { }

        protected internal virtual bool CanActivate(ExtensionEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Activating(ExtensionEventArgs args)
        { }

        protected internal virtual void Activated(ExtensionEventArgs args)
        { }

        protected internal virtual bool CanOpen(SlotEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Opening(SlotEventArgs args)
        { }

        protected internal virtual void Opened(SlotEventArgs args)
        { }

        protected internal virtual bool CanPlug(CompositionEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Plugging(CompositionEventArgs args)
        { }

        protected internal virtual void Plugged(CompositionEventArgs args)
        { }

        protected internal virtual bool CanTag(TagEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Tagging(TagEventArgs args)
        { }

        protected internal virtual void Tagged(TagEventArgs args)
        { }

        protected internal virtual bool CanDeactivate(ExtensionEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Deactivating(ExtensionEventArgs args)
        { }

        protected internal virtual void Deactivated(ExtensionEventArgs args)
        { }

        protected internal virtual bool CanClose(SlotEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Closing(SlotEventArgs args)
        { }

        protected internal virtual void Closed(SlotEventArgs args)
        { }

        protected internal virtual bool CanUnplug(CompositionEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Unplugging(CompositionEventArgs args)
        { }

        protected internal virtual void Unplugged(CompositionEventArgs args)
        { }

        protected internal virtual bool CanUntag(TagEventArgs args, out LogArgs log)
        {
            log = null;
            return true;
        }

        protected internal virtual void Untagging(TagEventArgs args)
        { }

        protected internal virtual void Untagged(TagEventArgs args)
        { }
    }
}
